package com.info33.platform.business.app.util.excel;

import io.swagger.annotations.ApiModelProperty;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ExportExcel {

    public static List<Excel> excels = new ArrayList<>();

    public static <T> void analysisExport(HttpServletResponse response, String title, List<T> list, Class<?> c){
        analysisExport(response,title,list,c,true);
    }

    public static <T> void analysisExport(HttpServletResponse response, String title, List<T> list, Class<?> c, boolean isLast) {
        Field[] arr = c.getDeclaredFields();
        List<String> rowNameList = new ArrayList<>();
        String[] rowsName = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            boolean annotationPresent = arr[i].isAnnotationPresent(ApiModelProperty.class);
            String name = "";
            if (!annotationPresent) {
                rowsName[i] = "序号";
                if(i==0){
                    name = "序号";
                }
            } else {
                arr[i].setAccessible(true);
                if(title.equals("退票统计") && arr[i].getAnnotation(ApiModelProperty.class).value().equals("开票金额")){
                    rowsName[i]="退票金额";
                }else {
                    rowsName[i] = arr[i].getAnnotation(ApiModelProperty.class).value();
                }
                name = rowsName[i];

            }
            if (!name.equals("委外类型ID(0为其他)"))
                rowNameList.add(name);
        }

        List<Object[]> dataList = new ArrayList<>();
        for (int j = 0; j < list.size(); j++) {
            T item = list.get(j);
            Class<?> itemClass = item.getClass();
            Field[] fields = itemClass.getDeclaredFields();
            List<Object> objList = new ArrayList<>();
            Object[] objs = new Object[fields.length];
            for (int i = 0; i < fields.length; i++) {
                boolean annotationPresent = arr[i].isAnnotationPresent(ApiModelProperty.class);
                if (!annotationPresent) {
                    objs[i] = j + 1;
                    if(i==0){
                        objList.add(objs[i]);
                    }
                }else {
                    String name = arr[i].getAnnotation(ApiModelProperty.class).value();
                    if (!name.equals("委外类型ID(0为其他)")){
                        fields[i].setAccessible(true);
                        try {
                            Object o = fields[i].get(item);
                            //判断时间类型的字段
                            if (fields[i].getType() == Date.class) {
//                                if(!name.equals("操作时间")&&!name.equals("生产日期")){
//                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                                    if(o != null){
//                                        o = sdf.format((Date) o);
//                                    }
//                                }else{
                                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                    if(o != null){
                                        o = sdf.format((Date) o);
                                    }
//                                }
                            }
                            if(o==null || o.toString().equals("")){
                                o = "无";
                            }
//                            //判断审核状态字段
//                            if (name.contains("审核状态")) {
//                                Integer auditStatus = (Integer) o;
//                                if (auditStatus.equals(AuditStatusEnum.AUDIT_WAIT.getCode())){
//                                    o = AuditStatusEnum.AUDIT_WAIT.getMessage();
//                                }else if (auditStatus.equals(AuditStatusEnum.AUDIT_PASS.getCode())){
//                                    o = AuditStatusEnum.AUDIT_PASS.getMessage();
//                                }else if (auditStatus.equals(AuditStatusEnum.AUDIT_FAILURE.getCode())){
//                                    o = AuditStatusEnum.AUDIT_FAILURE.getMessage();
//                                }else {
//                                    o = AuditStatusEnum.AMEND.getMessage();
//                                }
//                            }
                            objs[i] = o;
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        objList.add(objs[i]);
                    }
                }
            }
            Object[] objects = objList.toArray();
            dataList.add(objects);
        }
        String[] rowNameArray = rowNameList.toArray(new String[0]);
        Excel excel = new Excel();
        excel.setTitle(title);
        excel.setRowName(rowNameArray);
        excel.setDataList(dataList);
        excels.add(excel);
        if (isLast){
            export(response);
            excels = new ArrayList<>();
        }
    }

    public static void export(HttpServletResponse response) {
        String fileName = String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
        //告诉浏览器数据格式，将头和数据传到前台
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");

        //调用poi的工具类
        ExportExcelUtil ex = new ExportExcelUtil(excels);
        try {
            OutputStream out = response.getOutputStream();
            ex.export(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
